<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'helpvideo:helpCenterInfo:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <div class="fireVeIm">
            <div class="fireVideo">
                <span>
                    认证视频：
                </span>
                <video
                        width="100%"
                        height="100%"
                        :src="resVideoUrl"
                        controls
                        autoplay
                        muted
                        @loadeddata="onVideoLoaded"
                        @error="onVideoError"
                        type="video/mp4">
                </video>
            </div>
        </div>
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsHelpvideoHelpCenterInfoForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { HelpCenterInfo, helpCenterInfoSave, helpCenterInfoForm } from '/@/api/helpCenter/helpCenter';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('helpvideo.helpCenterInfo');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<HelpCenterInfo>({} as HelpCenterInfo);

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增') : t('编辑'),
  }));

  const resVideoUrl=ref('')

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('模块对象'),
      field: 'moduleType',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    {
      label: t('标题'),
      field: 'title',
      component: 'Input',
      componentProps: {
        maxlength: 255,
      },
      required: true,
    },
    {
      label: t('描述内容'),
      field: 'description',
      component: 'Input',
    },
    {
      label: t('视频地址'),
      field: 'videoUrl',
      component: 'Input',
      componentProps:{
        disabled:true,
      }
    },
    {
      label: t('创建时间'),
      field: 'createTime',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      required: true,
    },
    {
      label: t('视频上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        fileUrl: computed(() => record.value.videoUrl),
        bizType: 'helpCenterInfo_file',
        uploadType: 'all',
        accept:['mp4,avi,flv,rmvb,wmv,rm']
      },
      colProps: { md: 24, lg: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await helpCenterInfoForm(data);
    console.log('res', res);
    resVideoUrl.value = res.helpCenterInfo.videoUrl
    record.value = (res.helpCenterInfo || {}) as HelpCenterInfo;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      data={...data,videoUrl:(data.dataMap.helpCenterInfo_fileUrl != null) ? (data.dataMap.helpCenterInfo_fileUrl.split('?')[0]) : record.value.videoUrl,}
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        tid: record.value.tid,
      };
      // console.log('submit', params, data, record);
      const res = await helpCenterInfoSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(error.message || t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
<style scoped lang="less">
.fireVeIm {
 margin-left: 3.5%;
}
.fireVideo {
  // width: 20%;
  height: 400px;
}
</style>